package plus.easydo.starter.mybatis.plus.qo;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import plus.easydo.starter.mybatis.plus.page.PageSupport;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author laoyu
 * @version 1.0
 */
@Data
@SuperBuilder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class MpBaseQo implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "当前页")
    protected Integer pageNum = 1;

    @ApiModelProperty(value = "每页显示条数")
    protected Integer pageSize = 10;

    @ApiModelProperty(value = "排序字段集合")
    protected List<OrderItem> orders;

    @ApiModelProperty(value = "自定义查询参数")
    protected Map<String,Object> params;


    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public List<OrderItem> getOrders() {
        return orders;
    }

    public void setOrders(List<OrderItem> orders) {
        this.orders = orders;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public <T> IPage<T> startPage(){
        Page<T> page = new Page<>(this.pageNum, this.pageSize);
        PageSupport pageSupport = SpringUtil.getBean(PageSupport.class);
        if(Objects.nonNull(pageSupport) && pageSupport.getEnable()){
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = servletRequestAttributes.getRequest();
            String pageNumFiled = pageSupport.getPageNumFiled();
            String pageSizeFiled = pageSupport.getPageSizeFiled();
            Map<String, String[]> map = request.getParameterMap();
            String current = request.getParameter(pageNumFiled);
            String size = request.getParameter(pageSizeFiled);
            if(Objects.nonNull(current) && Objects.nonNull(size)){
                page = new Page<>(Long.parseLong(current),Long.parseLong(size));
            }
        }
        page.setOrders(this.orders);
        return page;
    }



}
